package Janelas.SQL;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.JButton;

import Banco.ConnectionFactory;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class JanelaTrfDrg extends JFrame {

	private static final long serialVersionUID = 2L;
	
	private JPanel contentPane;
	private JTextField nomeTrfDrg;
	private JTextField CPFTrfDrg;
	private JTextField RGTrfDrg;
	private JTextField estdTrfDrg;
	private JTextField dataNascTrfDrg;
	private JTextField lrgTrfDrg;
	private JTextField numTrfDrg;
	private JTextField bairro;
	private JTextField complTrfDrg;
	private JTextField CEPTrfDrg;
	private JTextField telResTrfDrg;
	private JTextField telCelTrfDrg;
	private JTextField cityTrfDrg;
	private JTextField codTrfDrg;

	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					JanelaTrfDrg frame = new JanelaTrfDrg();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	public JanelaTrfDrg() {
		setTitle("Tr\u00E1fico de Drogas - Occurence Safe");
		setResizable(false);
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 700, 350);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel label = new JLabel("Occurence Safe");
		label.setFont(new Font("Trebuchet MS", Font.BOLD | Font.ITALIC, 20));
		label.setBounds(506, 11, 168, 24);
		contentPane.add(label);
		
		JLabel label_1 = new JLabel("Para continuar o cadastro, preencha os campos abaixo");
		label_1.setFont(new Font("Tahoma", Font.PLAIN, 14));
		label_1.setBounds(10, 19, 391, 14);
		contentPane.add(label_1);
		
		JLabel label_2 = new JLabel("Dados do suspeito");
		label_2.setFont(new Font("Tahoma", Font.BOLD, 13));
		label_2.setBounds(10, 90, 168, 14);
		contentPane.add(label_2);
		
		JLabel label_3 = new JLabel("Nome:");
		label_3.setBounds(10, 125, 61, 14);
		contentPane.add(label_3);
		
		nomeTrfDrg = new JTextField();
		nomeTrfDrg.setColumns(10);
		nomeTrfDrg.setBounds(64, 122, 610, 20);
		contentPane.add(nomeTrfDrg);
		
		JLabel label_4 = new JLabel("CPF:");
		label_4.setBounds(20, 150, 46, 14);
		contentPane.add(label_4);
		
		CPFTrfDrg = new JTextField();
		CPFTrfDrg.setColumns(10);
		CPFTrfDrg.setBounds(64, 147, 160, 20);
		contentPane.add(CPFTrfDrg);
		
		JLabel label_5 = new JLabel("RG:");
		label_5.setBounds(234, 150, 46, 14);
		contentPane.add(label_5);
		
		RGTrfDrg = new JTextField();
		RGTrfDrg.setColumns(10);
		RGTrfDrg.setBounds(269, 147, 132, 20);
		contentPane.add(RGTrfDrg);
		
		estdTrfDrg = new JTextField();
		estdTrfDrg.setColumns(10);
		estdTrfDrg.setBounds(539, 147, 135, 20);
		contentPane.add(estdTrfDrg);
		
		JLabel label_7 = new JLabel("Data de Nascimento:");
		label_7.setBounds(10, 175, 122, 14);
		contentPane.add(label_7);
		
		dataNascTrfDrg = new JTextField();
		dataNascTrfDrg.setColumns(10);
		dataNascTrfDrg.setBounds(147, 172, 158, 20);
		contentPane.add(dataNascTrfDrg);
		
		JLabel label_8 = new JLabel("Logradouro:");
		label_8.setBounds(10, 200, 76, 14);
		contentPane.add(label_8);
		
		lrgTrfDrg = new JTextField();
		lrgTrfDrg.setColumns(10);
		lrgTrfDrg.setBounds(96, 197, 397, 20);
		contentPane.add(lrgTrfDrg);
		
		JLabel label_9 = new JLabel("N\u00FAmero:");
		label_9.setBounds(532, 200, 76, 14);
		contentPane.add(label_9);
		
		numTrfDrg = new JTextField();
		numTrfDrg.setColumns(10);
		numTrfDrg.setBounds(588, 197, 86, 20);
		contentPane.add(numTrfDrg);
		
		JLabel label_10 = new JLabel("Bairro:");
		label_10.setBounds(10, 225, 46, 14);
		contentPane.add(label_10);
		
		bairro = new JTextField();
		bairro.setColumns(10);
		bairro.setBounds(64, 222, 132, 20);
		contentPane.add(bairro);
		
		JLabel label_11 = new JLabel("Complemento:");
		label_11.setBounds(217, 225, 88, 14);
		contentPane.add(label_11);
		
		complTrfDrg = new JTextField();
		complTrfDrg.setColumns(10);
		complTrfDrg.setBounds(304, 222, 218, 20);
		contentPane.add(complTrfDrg);
		
		JLabel label_12 = new JLabel("CEP:");
		label_12.setBounds(554, 225, 46, 14);
		contentPane.add(label_12);
		
		CEPTrfDrg = new JTextField();
		CEPTrfDrg.setColumns(10);
		CEPTrfDrg.setBounds(588, 222, 86, 20);
		contentPane.add(CEPTrfDrg);
		
		JLabel label_13 = new JLabel("Telefone Residencial:");
		label_13.setBounds(10, 250, 132, 14);
		contentPane.add(label_13);
		
		telResTrfDrg = new JTextField();
		telResTrfDrg.setColumns(10);
		telResTrfDrg.setBounds(141, 247, 199, 20);
		contentPane.add(telResTrfDrg);
		
		telCelTrfDrg = new JTextField();
		telCelTrfDrg.setColumns(10);
		telCelTrfDrg.setBounds(475, 247, 199, 20);
		contentPane.add(telCelTrfDrg);
		
		JLabel label_14 = new JLabel("Telefone Celular:");
		label_14.setBounds(370, 250, 95, 14);
		contentPane.add(label_14);
		
		JButton button = new JButton("Cancelar");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				dispose();
			}
		});
		button.setBounds(10, 278, 89, 23);
		contentPane.add(button);
		
		JButton button_1 = new JButton("Cadastrar Ocorr\u00EAncia");
		button_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Connection con;
				String query;
				query = "INSERT INTO AREA_TRAFICO_DE_DROGAS (ID_OCORRENCIA, CODIGO_TRAFICO) VALUES (?,?)";
				try {

					con = ConnectionFactory.getConnection();
					PreparedStatement stmt = con.prepareStatement(query);
					stmt.setString(1, "");
					stmt.setString(2, codTrfDrg.getText());
					stmt.executeUpdate();
					stmt.close();
					con.close();

				} catch (SQLException e1) {

					e1.printStackTrace();
				}

					
				query = "INSERT INTO SUSPEITO (ID_OCORRENCIA, NOME_SUSPEITOS, CPF_SUSPEITO, RG_SUSPEITOS, DATA_NASC_SUSPEITOS) VALUES (?,?,?,?,?)";

				try {

					con = ConnectionFactory.getConnection();
					PreparedStatement stmt = con.prepareStatement(query);
					stmt.setString(1, "");
					stmt.setString(2, nomeTrfDrg.getText());
					stmt.setString(3, CPFTrfDrg.getText());
					stmt.setString(4, RGTrfDrg.getText());
					stmt.setString(5, dataNascTrfDrg.getText());

					stmt.executeUpdate();
					stmt.close();
					con.close();

				} catch (SQLException e1) {

					e1.printStackTrace();
				}

				query = "INSERT INTO ENDERECO (ID_OCORRENCIA,LOGRADOURO, NUMERO, BAIRRO, COMPLEMENTO, CIDADE, ESTADO, CEP) VALUES (?,?,?,?,?,?,?)";

				try {
					con = ConnectionFactory.getConnection();
					PreparedStatement stmt = con.prepareStatement(query);
					stmt.setString(1, "");
					stmt.setString(2, lrgTrfDrg.getText());
					stmt.setString(3, numTrfDrg.getText());
					stmt.setString(4, bairro.getText());
					stmt.setString(5, complTrfDrg.getText());
					stmt.setString(6, cityTrfDrg.getText());
					stmt.setString(7, estdTrfDrg.getText());
					stmt.setString(8, CEPTrfDrg.getText());

					stmt.executeUpdate();
					stmt.close();
					con.close();

				} catch (SQLException e1) {
					e1.printStackTrace();
				}
								query = "INSERT INTO TELEFONES (ID_OCORRENCIA, FONE1, FONE2, FONE3) VALUES (?,?,?)";
				try {
					con = ConnectionFactory.getConnection();
					PreparedStatement stmt = con.prepareStatement(query);
					stmt.setString(1, "");
					stmt.setString(1, telResTrfDrg.getText());
					stmt.setString(2, telCelTrfDrg.getText());

					stmt.executeUpdate();
					stmt.close();
					con.close();

				} catch (SQLException e1) {
					e1.printStackTrace();
				}
				nomeTrfDrg.getText().toString().trim();
				CPFTrfDrg.getText().toString().trim();
				RGTrfDrg.getText().toString().trim();
				estdTrfDrg.getText().toString().trim();
				dataNascTrfDrg.getText().toString().trim();
				lrgTrfDrg.getText().toString().trim();
				numTrfDrg.getText().toString().trim();
				bairro.getText().toString().trim();
				complTrfDrg.getText().toString().trim();
				CEPTrfDrg.getText().toString().trim();
				telResTrfDrg.getText().toString().trim();
				telCelTrfDrg.getText().toString().trim();
				
				JOptionPane.showMessageDialog(null, "Cadastro realizado com sucesso!");
				
				dispose();
			}
		});
		button_1.setBounds(516, 278, 158, 23);
		contentPane.add(button_1);
		
		JLabel lblEstado = new JLabel("Estado:");
		lblEstado.setBounds(488, 150, 46, 14);
		contentPane.add(lblEstado);
		
		JLabel lblCidade = new JLabel("Cidade:");
		lblCidade.setBounds(400, 175, 46, 14);
		contentPane.add(lblCidade);
		
		cityTrfDrg = new JTextField();
		cityTrfDrg.setColumns(10);
		cityTrfDrg.setBounds(456, 172, 218, 20);
		contentPane.add(cityTrfDrg);
		
		JLabel lblCdigoDaOcorrncia = new JLabel("C\u00F3digo da ocorr\u00EAncia");
		lblCdigoDaOcorrncia.setFont(new Font("Tahoma", Font.BOLD, 13));
		lblCdigoDaOcorrncia.setBounds(10, 37, 168, 24);
		contentPane.add(lblCdigoDaOcorrncia);
		
		JLabel label_6 = new JLabel("C\u00F3digo:");
		label_6.setBounds(10, 62, 46, 14);
		contentPane.add(label_6);
		
		codTrfDrg = new JTextField();
		codTrfDrg.setColumns(10);
		codTrfDrg.setBounds(66, 59, 86, 20);
		contentPane.add(codTrfDrg);
	}
}
